<?php

namespace App\Http\Resources;

use App\Models\CompanyModel;
use App\Models\JobpositionModel;
use App\Models\PlazaModel;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Collection;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class JobpositionResource extends ResourceCollection
{

    /**
     * Transform the resource into an array.
     * 职位列表详情   收藏的职位详情
     */
    public function toArray($request)
    {
        return [
            'item_list' => $this->collectionList($this->collection),
            'total' => $this->total(),
            'page' => $this->currentPage(),
            'count' => $this->count(),
            'is_next' => $this->hasMorePages(),
            'last_page' => $this->lastPage(),
        ];
    }

    private function collectionList(Collection $collection)
    {
        $list = [];
        foreach ($collection as $item) {
            $list[] = [
                'id' => $item->id,
                'job_position_id' => $item->id,
                'position_name' => $item->position_name,
                'salary' => (string)$item->salary,
                'salary_type' => JobpositionModel::SALARY_TYPE_LIST[$item->salary_type],
                'tags' => $item->tags,
                'label' => JobpositionModel::LABEL_LIST[$item->label]??'',
                'title_tag' => JobpositionModel::TITLE_TAG[$item->title_tag]??'',
//                'work_location' =>$item->work_location,
//                'work_date' =>$item->work_date,

//                'company_name' =>$item->company->company_name ?: '个体招聘',
                'company_name' => $item->company->cur_type == CompanyModel::TYPE_ENTERPRISE?$item->company->company_name:'个体招聘',
                //'company_name' =>$item->company->company_name ?? '',
                'contact_person_avatar' =>$item->company->contactPersonUrl(),
                'contact_person_name' =>$item->company->contact_person_name??'',
                'region' =>$this->region($item->region_city,$item->region_region),
            ];
        }
        return $list;
    }
    private function region($region_city,$region_region)
    {
        if (!$region_region){
            return '不限地点';
        }
        return DcatDistpickerHelper::getAreaName($region_city) . DcatDistpickerHelper::getAreaName($region_region);
    }
}
